home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 6 code / TCP / NewsWatcher / NW Source / Shared Code / Reusable Source / popuputil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-05  |  6.5 KB  |  240 lines  |  [TEXT/MMCC]

  1. /*----------------------------------------------------------------------------
  2.  
  3.     popuputil.c
  4.  
  5.     This reusable module contains miscellaneous popup menu utility routines.
  6.     
  7.     Copyright © 1994-1995, Northwestern University.
  8.  
  9. ----------------------------------------------------------------------------*/
  10.  
  11. #include "def.h"
  12. #include "popuputil.h"
  13.  
  14.  
  15.  
  16. /*----------------------------------------------------------------------------
  17.     GetPopupPString
  18.     
  19.     Get the text of an item in a Popup Menu control, as a Pascal format
  20.     string.
  21.     
  22.     Entry:    ctl = handle to popup menu control.
  23.             item = item number to get, or kCurrentPopupItem for 
  24.                 the currently selected item.
  25.                 
  26.     Exit:    str = the item text, as a Pascal format string.
  27. ----------------------------------------------------------------------------*/
  28.  
  29. void GetPopupPString (ControlHandle    ctl, short item, Str255    str)
  30. {
  31.     popupPrivateData **data;
  32.     
  33.     data = (popupPrivateData**)(**ctl).contrlData;
  34.     if (item == kCurrentPopupItem) item = GetControlValue(ctl);
  35.     GetMenuItemText((**data).mHandle, item, str);
  36. }
  37.  
  38.  
  39.  
  40. /*----------------------------------------------------------------------------
  41.     GetPopupCString
  42.     
  43.     Get the text of an item in a Popup Menu control, as a C format
  44.     string.
  45.     
  46.     Entry:    ctl = handle to popup menu control.
  47.             item = item number to get, or kCurrentPopupItem for 
  48.                 the currently selected item.
  49.                 
  50.     Exit:    str = the item text, as a C format string.
  51. ----------------------------------------------------------------------------*/
  52.  
  53. void GetPopupCString (ControlHandle ctl, short item, char *str)
  54. {
  55.     GetPopupPString(ctl, item, (StringPtr)str);
  56.     p2cstr((StringPtr)str);
  57. }
  58.  
  59.  
  60.  
  61. /*----------------------------------------------------------------------------
  62.     SetPopupItemStyle
  63.     
  64.     Set the text style for one of the items in a popup menu control.
  65.     
  66.     Entry:    ctl = handle to popup menu control.
  67.             item = item number to set, or kCurrentPopupItem for 
  68.                 the currently selected item.
  69.             style = the new text style.
  70. ----------------------------------------------------------------------------*/
  71.  
  72. void SetPopupItemStyle (ControlHandle ctl, short item, short style)
  73. {
  74.     popupPrivateData **data;
  75.     
  76.     data = (popupPrivateData**)(**ctl).contrlData;
  77.     if (item == kCurrentPopupItem) item = GetControlValue(ctl);
  78.     SetItemStyle((**data).mHandle, item, style);
  79. }
  80.  
  81.  
  82.  
  83. /*----------------------------------------------------------------------------
  84.     AddPopupItem
  85.     
  86.     Add an item to a popup menu control.
  87.     
  88.     Entry:    ctl = handle to popup menu control.
  89.             after = item number after which the new item should be
  90.                 inserted, or 0 to insert the new item and the beginning
  91.                 of the menu.
  92.             str = text or the new item, in Pascal format.
  93. ----------------------------------------------------------------------------*/
  94.  
  95. void AddPopupItem (ControlHandle ctl, short after, Str255 str)
  96. {
  97.     popupPrivateData **data;
  98.     short curValue;
  99.  
  100.     data = (popupPrivateData**)(**ctl).contrlData;
  101.     InsertMenuItem((**data).mHandle, str, after);
  102.     curValue = GetControlValue(ctl);
  103.     if (after < curValue) SetControlValue(ctl, curValue+1);
  104. }
  105.  
  106.  
  107.  
  108. /*----------------------------------------------------------------------------
  109.     DelPopupItem
  110.     
  111.     Delete an item from a popup menu control.
  112.     
  113.     Entry:    ctl = handle to popup menu control.
  114.             item = item number to delete, or kCurrentPopupItem to delete 
  115.                 the currently selected item.
  116. ----------------------------------------------------------------------------*/
  117.  
  118. void DelPopupItem (ControlHandle ctl, short item)
  119. {
  120.     popupPrivateData **data;
  121.     short curValue;
  122.  
  123.     data = (popupPrivateData**)(**ctl).contrlData;
  124.     curValue = GetControlValue(ctl);
  125.     if (item == kCurrentPopupItem) item = curValue;
  126.     DeleteMenuItem((**data).mHandle, item);
  127.     if (item < curValue) SetControlValue(ctl, curValue-1);
  128. }
  129.  
  130.  
  131. /*----------------------------------------------------------------------------
  132.     SetPopupValue
  133.     
  134.     Set the value of a popup menu control to the item matching a string.
  135.     
  136.     Entry:    ctl = handle to popup menu control.
  137.             str = Pascal format string.
  138.             isNumber = true if string and menu items are numbers.
  139.  
  140.     Exit:    function result = new control value, or 0 if no matching
  141.                 item.
  142. ----------------------------------------------------------------------------*/
  143.  
  144. short SetPopupValue (ControlHandle ctl, Str255 str, Boolean    isNumber)
  145. {
  146.     popupPrivateData **data;
  147.     long checkVal, itemVal;
  148.     MenuHandle menu;
  149.     short numItems, item;
  150.     Str255 tempStr;
  151.  
  152.     data = (popupPrivateData**)(**ctl).contrlData;
  153.     if (isNumber) StringToNum(str, &checkVal);
  154.     menu = (**data).mHandle;
  155.  
  156.     numItems = CountMItems(menu);
  157.     for (item = 1; item <= numItems; item++) {
  158.         GetMenuItemText(menu, item, tempStr);
  159.         if (isNumber) {
  160.             StringToNum(tempStr, &itemVal);
  161.             if (checkVal == itemVal) {
  162.                 SetControlValue(ctl, item);
  163.                 return item;
  164.             }
  165.         } else if (EqualString(str, tempStr, false, true)) {
  166.             SetControlValue(ctl, item);
  167.             return item;
  168.         }
  169.     }
  170.     return 0;
  171. }
  172.  
  173.  
  174.  
  175. /*----------------------------------------------------------------------------
  176.     TrackPopup
  177.     
  178.     Track a click in a typein popup menu control.
  179.     
  180.     Entry:    ctl = handle to popup menu control.
  181.             where = location of mouse click, in local coords.
  182.             checkItem = text of the item that should appear checked
  183.                 (the contents of the "partner" typein textedit field),
  184.                 in Pascal format.
  185.             isNumber = true if menu items are numbers.
  186.             
  187.     Exit:    function result = new control value, or 0 if no change.
  188.  
  189.     If the text does not match a menu item, a new item is added
  190.     at the beginning, followed by a separator line. These two extra
  191.     items are deleted before the function returns. 
  192. ----------------------------------------------------------------------------*/
  193.  
  194. short TrackPopup (ControlHandle    ctl, Point where, Str255 checkItem, Boolean    isNumber)
  195. {
  196.     popupPrivateData **data;
  197.     MenuHandle menu;
  198.     long checkVal;
  199.     short i, itemsAdded, part, newValue, oldValue;
  200.     Str255 tempStr;
  201.  
  202.     data = (popupPrivateData **)(**ctl).contrlData;
  203.     menu = (**data).mHandle;
  204.     itemsAdded = 0;
  205.     if (checkItem && *checkItem) {
  206.         oldValue = SetPopupValue(ctl, checkItem, isNumber);
  207.         if (oldValue == 0) {
  208.             if (isNumber) {
  209.                 StringToNum(checkItem, &checkVal);
  210.                 NumToString(checkVal, tempStr);
  211.                 InsertMenuItem(menu, tempStr, 0);
  212.             } else {
  213.                 InsertMenuItem(menu, checkItem, 0);
  214.             }
  215.             InsertMenuItem(menu, "\p(-", 1);
  216.             oldValue = 1;
  217.             itemsAdded = 2;
  218.             SetControlValue(ctl, oldValue);
  219.         }
  220.     } else {
  221.         oldValue = 0;
  222.     }
  223.  
  224.     part = TrackControl(ctl, where, (ControlActionUPP)-1);
  225.     newValue = GetControlValue(ctl);
  226.  
  227.     if (part != 0 && oldValue != newValue) {
  228.         newValue = newValue - itemsAdded;
  229.     } else {
  230.         newValue = 0;
  231.     }
  232.     if (itemsAdded) {
  233.         for (i = 0; i < itemsAdded; i++) {
  234.             DeleteMenuItem(menu, 1);
  235.         }
  236.         SetControlValue(ctl, newValue);
  237.     }
  238.     return newValue;
  239. }
  240.